package cn.jtool.dig.server.controller;

import cn.jtool.dig.server.domain.reqeust.LicenseParams;
import cn.jtool.dig.server.domain.reqeust.LicenseQueryRequest;
import cn.jtool.dig.server.service.LicenseService;
import cn.jtool.knife.core.result.ResultEntity;
import cn.jtool.knife.core.validator.Create;
import jakarta.annotation.Resource;
import org.springframework.data.repository.query.Param;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/license")
public class LicenseController {

    @Resource
    private LicenseService licenseService;

    @PostMapping(value = "/create", name = "创建授权码", produces = "application/json")
    public ResponseEntity<ResultEntity<?>> create(@Validated(value = Create.class) @RequestBody LicenseParams entity){
        this.licenseService.create(entity);
        return ResponseEntity.ok(
                ResultEntity.success()
        );
    }

    @PutMapping(value = "/update", name = "更新授权码", produces = "application/json")
    public ResponseEntity<ResultEntity<?>> update(@Validated(value = Create.class) @RequestBody LicenseParams entity){
        this.licenseService.update(entity.getId(), entity);
        return ResponseEntity.ok(
                ResultEntity.success()
        );
    }

    @DeleteMapping(value = "/delete", name = "删除授权码", produces = "application/json")
    public ResponseEntity<ResultEntity<?>> delete(@RequestParam(value = "id") int id){
        this.licenseService.delete(id);
        return ResponseEntity.ok(
                ResultEntity.success()
        );
    }

    @GetMapping(value = "/detail", name = "获取授权码详情", produces = "application/json")
    public ResponseEntity<ResultEntity<?>> detail(@RequestParam(value = "id") int id){
        return ResponseEntity.ok(
                ResultEntity.success(
                        this.licenseService.selectOne(id)
                )
        );
    }

    @GetMapping(value = "/list", name = "获取授权码列表", produces = "application/json")
    public ResponseEntity<ResultEntity<?>> list(@RequestParam(value = "page", defaultValue = "1") int page,
                                                @RequestParam(value = "limit", defaultValue = "10") int limit){
        return ResponseEntity.ok(
                ResultEntity.success(
                        this.licenseService.listByPage(new LicenseQueryRequest(), page, limit)
                )
        );
    }


}
